package zjs.dc.controller;

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

/**
 * @Class:
 * @Author: kongcb
 * @Description: TODO
 * @Date: 2021/12/15 9:25
 */
public class TTaxCredit {
    public static void main(String[] args) {
        SparkSession spark = SparkSession.builder().appName("t_taxcredit").enableHiveSupport().getOrCreate();
        spark.sparkContext().setLogLevel("ERROR");
        Dataset<Row> zs_data = spark.sql("select * from a_dc_ep_ods.zs_t_tax_credit");
        long zs_count = zs_data.count();
        if(zs_count>0){

            //企查查数据获取统一社会信用代码
            spark.sql("select a.* ,b.credit_code from a_dc_ep_dwi.t_taxcredit a left join a_dc_ep_ods.t_eci_company b on a.company_id = b.company_id")
                    .createOrReplaceTempView("qcc_taxcredit");
            //中数获取统一社会信用代码
            spark.sql("select a.*,del.creditcode from a_dc_ep_ods.zs_t_tax_credit a " +
                    "left join a_dc_ep_ods.zs_t_tax_credit_del del on a.entid = del.entid where del.entid is not null").createOrReplaceTempView("zs_taxcredit");
            spark.sql("select a.key_no,a.company_id,a.company_name,a.province,b.tax_no,a.credit_code from \n" +
                    "(select c.key_no,c.company_id,c.company_name,c.province,c.credit_code from a_dc_ep_ods.t_eci_company c where c.credit_code in \n" +
                    "(select d.creditcode from a_dc_ep_ods.zs_t_tax_credit_del d where d.creditcode !='' and d.creditcode is not null)) a \n" +
                    "left join a_dc_ep_dwi.t_eci_companyattr b on a.company_id=b.company_id where a.credit_code is not null and  a.credit_code!=''").createOrReplaceTempView("qcc_company");
            //新增
            spark.sql("insert overwrite table a_dc_ep_incr.t_taxcredit select " +
                    "zs.RECORD_ID," +
                    "qcc.key_no," +
                    "qcc.company_id," +
                    "qcc.company_name," +
                    "qcc.tax_no," +
                    "qcc.province," +
                    "zs.EVA_YEAR," +
                    "zs.RATE," +
                    "zs.jobid," +
                    "'1' as isadd " +
                    "from zs_taxcredit zs left join qcc_company qcc on zs.creditcode = qcc.credit_code where qcc.credit_code is not null ");
            //删除
            spark.sql("insert into table a_dc_ep_incr.t_taxcredit select " +
                    "qcc.id,\n" +
                    "qcc.key_no,\n" +
                    "qcc.company_id,\n" +
                    "qcc.company_name,\n" +
                    "qcc.no,\n" +
                    "qcc.province,\n" +
                    "qcc.year,\n" +
                    "qcc.level," +
                    "qcc.dates," +
                    "'-1' as isadd " +
                    "from qcc_taxcredit qcc inner join (select a.* from a_dc_ep_ods.zs_t_tax_credit_del a  where a.creditcode is not null and a.creditcode != '') del on upper(trim(qcc.credit_code)) = upper(trim(del.creditcode))");

        }else {
            spark.sql("TRUNCATE TABLE a_dc_ep_incr.t_taxcredit");
            System.out.println("中数本期数据涉及该表的数据为空。。。。。。");
        }
        spark.stop();
    }
}
